package com.mx.recycle_demo.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.mx.recycle_demo.R
import com.mx.recycleview.anytype.BaseTypeAdapt
import com.mx.recycleview.anytype.SingleLineAdapt
import com.mx.recycleview.anytype.TypeLayoutManager
import com.mx.recycleview.utils.Log
import kotlinx.android.synthetic.main.activity_any_type_recycle_view.*
import kotlinx.android.synthetic.main.adapt_simple_img.view.*

class AnyTypeActivity : AppCompatActivity() {
    private val typeLayoutManager by lazy { TypeLayoutManager(this) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_any_type_recycle_view)
        typeLayoutManager.setAdapts(createAdapts())
        typeLayoutManager.attachToRecycleView(recycleView)
    }

    private fun createAdapts(): List<BaseTypeAdapt<*>> {
        val adapts = arrayListOf<BaseTypeAdapt<*>>()
        val first = object : SingleLineAdapt() {
            override fun createItem(inflater: LayoutInflater, parent: ViewGroup): View {
                return inflater.inflate(R.layout.adapt_simple_img, parent, false)
            }

            override fun bindView(itemView: View) {
                itemView.image.setImageResource(R.mipmap.item1)
            }
        }
        val second = object : BaseTypeAdapt<Int>(
            arrayListOf(
                R.mipmap.item1,
                R.mipmap.item2,
                R.mipmap.item3,
                R.mipmap.item4,
                R.mipmap.item5,
                R.mipmap.item3,
                R.mipmap.item4,
                R.mipmap.item4,
                R.mipmap.item5,
                R.mipmap.item3,
                R.mipmap.item4,
                R.mipmap.item5,
                R.mipmap.item6
            )
        ) {
            override fun createItem(
                inflater: LayoutInflater,
                parent: ViewGroup
            ): View {
                return inflater.inflate(R.layout.adapt_simple_img, parent, false)
            }

            override fun getSpanSize(): Int {
                return 8
            }

            override fun bindView(position: Int, itemView: View, record: Int) {
                itemView.image.setImageResource(record)
            }

        }
        adapts.add(first)
        adapts.add(second)
        first.showWithAdapt(second)

        second.setItemClick { index, record ->
            Log("点击：$index $record")
            second.list.removeAt(index)
            typeLayoutManager.notifyDataSetChanged()
        }

        adapts.add(object : BaseTypeAdapt<Int>(
            arrayListOf(
                R.mipmap.item1,
                R.mipmap.item2,
                R.mipmap.item3,
                R.mipmap.item4,
                R.mipmap.item5,
                R.mipmap.item6,
                R.mipmap.item1,
                R.mipmap.item4,
                R.mipmap.item5,
                R.mipmap.item6
            )
        ) {
            override fun createItem(
                inflater: LayoutInflater,
                parent: ViewGroup
            ): View {
                return inflater.inflate(R.layout.adapt_simple_img, parent, false)
            }

            override fun getSpanSize(): Int {
                return 1
            }

            override fun bindView(position: Int, itemView: View, record: Int) {
                itemView.image.setImageResource(record)
            }

        })
        adapts.add(object : BaseTypeAdapt<Int>(
            arrayListOf(
                R.mipmap.item1,
                R.mipmap.item2,
                R.mipmap.item3,
                R.mipmap.item4,
                R.mipmap.item5,
                R.mipmap.item6,
                R.mipmap.item1,
                R.mipmap.item4,
                R.mipmap.item5,
                R.mipmap.item6
            )
        ) {
            override fun createItem(
                inflater: LayoutInflater,
                parent: ViewGroup
            ): View {
                return inflater.inflate(R.layout.adapt_simple_img, parent, false)
            }

            override fun getSpanSize(): Int {
                return 2
            }

            override fun bindView(position: Int, itemView: View, record: Int) {
                itemView.image.setImageResource(record)
            }

        })
        adapts.add(object : SingleLineAdapt() {
            override fun createItem(inflater: LayoutInflater, parent: ViewGroup): View {
                return inflater.inflate(R.layout.adapt_simple_img, parent, false)
            }

            override fun bindView(itemView: View) {
                itemView.image.setImageResource(R.mipmap.item3)
            }
        })
        return adapts
    }

}